package com.xxx.common.controller;


import com.github.pagehelper.PageInfo;
import com.xxx.common.entity.Entrust;
import com.xxx.common.mapper.EntrustMapper;
import com.xxx.common.service.EntrustService;
import com.xxx.common.utils.Result;
import com.xxx.common.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/common/entrust")
public class EntrustController {
    
    @Autowired
    private EntrustService entrustService;


    @GetMapping("/getEntrustInfo")
    public Result getEntrustInfo(Entrust Entrust, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                 @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
        try{
            PageInfo<Entrust> data = entrustService.selectEntrustInfo(Entrust,pageNum,pageSize);
            return ResultUtil.success(1,"成功",data);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"失败");
        }
    }

    @PostMapping("/saveAndUpdateEntrustInfo")
    public Result saveAndUpdateEntrustInfo(Entrust Entrust){
        try {
            Integer count = entrustService.saveAndUpdateEntrustInfo(Entrust);
            return ResultUtil.success(1,"成功",count);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"失败");
        }
    }

    @DeleteMapping("/delEntrustInfo")
    public Result delEntrustInfo(Integer id){
        try {
            Integer count = entrustService.delEntrustInfo(id);
            return ResultUtil.success(1,"成功",count);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"失败");
        }
    }

    @Resource
    private EntrustMapper entrustMapper;

    @PutMapping("/updateBatchStatus")
    public Result updateBatchStatus(@RequestBody Entrust entrust){
        try {
            List<Integer> idList = entrust.getIdList();
            if (idList.size() > 0){
                entrustMapper.updateBatchStatus(idList);
            }
            return ResultUtil.success(1,"成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"失败");
        }
    }

}

